package HongPro;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class FileWrite {
	Socket socketFile;
	ClientFrame cf;

	public FileWrite(ClientFrame cf, Socket socketFile) {
		this.cf = cf;
		this.socketFile = socketFile;
	}

	public void sendMsg(String fileLocation) {
		BufferedOutputStream bout = null;
		FileInputStream fin = null;
		int n = 0;

		try {
			bout = new BufferedOutputStream(socketFile.getOutputStream());

			byte b[] = new byte[255];

			fin = new FileInputStream(fileLocation);
			while (true) {
				n = fin.read(b);
				if (n == -1) {
					cf.txtA.append("---------파일 전송완료--------\n");
					break;
				}
				bout.write(b, 0, n);
				bout.flush();
			}

		} catch (FileNotFoundException fe) {
			System.out.println(fe.getMessage());
		} catch (IOException ie) {
			System.out.println(ie.getMessage());
		} finally {
			try {
				if (bout != null)
					bout.close();
				if (fin != null)
					fin.close();
				if (socketFile != null)
					socketFile.close();
			} catch (IOException ie) {
				System.out.println(ie.getMessage());
			}
		}
	}
}
